<script setup lang="ts">
import { useCancelOrder, useDeleteOrder, useShowPrescription } from '@/composable'
import { OrderType } from '@/enums'

import type { ConsultOrderItem } from '@/types/consult'

// 接收list父组件中的item(ConsultOrderItem问诊订单单项信息)订单数据，来渲染页面
const props = defineProps<{
  item: ConsultOrderItem
}>()

// 1. body 区域，点击后去订单详情
// 2. foot 区域，根据订单状态展示按钮
// 待支付：取消问诊+去支付
// 待接诊：取消问诊+继续沟通
// 咨询中：查看处方（如果开了）+继续沟通
// 已完成：更多（查看处方，如果开了，删除订单）+问诊记录+（未评价?写评价:查看评价）
// 已取消：删除订单+咨询其他医生

// hook
/* // 更多 气泡弹出框
const showPopover = ref(false)
const actions = computed(() => [
  { text: '查看处方', disabled: !props.item.prescriptionId },
  { text: '删除订单' }
])
const onSelect = (action: { text: string }, i: number) => {
  // 点击了选项触发
  // i 索引
  if (i === 1) {
    deleteConsultOrder(props.item)
  }
  if (i === 0) {
    showPrescription(props.item.prescriptionId)
  }
} */

// 取消订单 封装
const { loading, onCancelOrder } = useCancelOrder()
/* const loading = ref(false)
const onCancelOrder = async (item: ConsultOrderItem) => {
  try {
    loading.value = true
    await cancelOrder(item.id)
    // 修改订单的状态
    item.status = OrderType.ConsultCancel
    item.statusValue = '已取消'
    Toast.success('取消成功')
  } catch (error) {
    Toast.fail('取消失败')
  } finally {
    loading.value = false
  }
} */

// 删除订单  封装

// 声明自定义事件，通知父组件，删除这条信息
const emit = defineEmits<{
  (e: 'on-delete', id: string): void
}>()

// 解构loading 重命名
const { loading: deleteLoading, deleteConsultOrder } = useDeleteOrder(() => {
  emit('on-delete', props.item.id)
})
// 加载效果，API函数，点击调用
/* const deleteLoading = ref(false)
const deleteConsultOrder = async (item: ConsultOrderItem) => {
  try {
    deleteLoading.value = true
    await deleteOrder(item.id)
    // 删除成功 把list的item删掉 删除这条信息
    emit('on-delete', item.id)
    Toast.success('删除成功')
  } catch (error) {
    Toast.fail('删除失败')
  } finally {
    deleteLoading.value = false
  }
} */

// 查看处方 hook
const { showPrescription } = useShowPrescription()
</script>

<template>
  <div class="consult-item">
    <!-- 卡片头部 张医生 咨询中 -->
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <!-- 极速问诊（自动分配医生） -->
      <p>{{ item.docInfo?.name }}</p>
      <!-- item.statusValue 状态的文字 颜色有变化 枚举类型中 OrderType 可以拿到问诊类型-->
      <span
        :class="{
          orange: item.status === OrderType.ConsultPay,
          green: item.status === OrderType.ConsultChat
        }"
        >{{ item.statusValue }}</span
      >
    </div>

    <!-- 病情描述 -->
    <div class="body" @click="$router.push(`/user/consult/${item.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ item.illnessDesc }}</div>
      </div>

      <!-- 价格 -->
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ item.payment }}</div>
      </div>

      <!-- 创建时间 -->
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ item.createTime }}</div>
      </div>
    </div>

    <!-- 底部按钮   -->
    <!-- 待支付：取消问诊+去支付(详情页里有支付按钮) -->
    <div class="foot" v-if="item.status === OrderType.ConsultPay">
      <van-button
        class="gray"
        plain
        size="small"
        round
        @click="onCancelOrder(item)"
        :loading="loading"
        >取消问诊</van-button
      >
      <van-button type="primary" plain size="small" round :to="`/user/consult/${item.id}`"
        >去支付</van-button
      >
    </div>
    <!-- 待接诊：取消问诊+继续沟通(去问诊室) -->
    <div class="foot" v-if="item.status === OrderType.ConsultWait">
      <van-button
        class="gray"
        plain
        size="small"
        round
        @click="onCancelOrder(item)"
        :loading="loading"
        >取消问诊</van-button
      >
      <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 咨询中：查看处方（如果开了）+继续沟通(去问诊室) -->
    <div class="foot" v-if="item.status === OrderType.ConsultChat">
      <van-button
        class="gray"
        plain
        size="small"
        round
        v-if="item.prescriptionId"
        @click="showPrescription(item.prescriptionId)"
        >查看处方</van-button
      >
      <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 已完成：更多（查看处方，如果开了，删除订单）+问诊记录+（未评价?写评价:查看评价） -->
    <div class="foot" v-if="item.status === OrderType.ConsultComplete">
      <!-- 更多操作 -->
      <!-- 更多组件 传一个disabled 如果没有处方id ， 就禁用disabled查看处方-->
      <cp-consult-more
        :disabled="!item.prescriptionId"
        @on-delete="deleteConsultOrder(item)"
        @on-preview="showPrescription(item.prescriptionId)"
      ></cp-consult-more>

      <van-button class="gray" plain size="small" round :to="`/room?orderId=${item.id}`"
        >问诊记录</van-button
      >
      <van-button type="primary" plain size="small" round v-if="!item.evaluateId"
        >写评价</van-button
      >
      <van-button class="gray" plain size="small" round v-else>查看评价</van-button>
    </div>
    <!-- 已取消：删除订单+咨询其他医生 -->
    <div class="foot" v-if="item.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="deleteLoading"
        @click="deleteConsultOrder(item)"
        >删除订单</van-button
      >
      <van-button type="primary" plain size="small" round to="/">咨询其他医生</van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
    .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    }
  }
}
</style>
